﻿using Super_Management_Mayhem.Content.Spritesheet;
using Super_Management_Mayhem.GameRoot;
using Super_Management_Mayhem.Screens.Playing.GameMode;
using Transformable_Engine_v2.Engine.Animation.ScaleAnimation;
using Transformable_Engine_v2.Engine.Animation.Trajectory;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.GameObjects.Containers.Transformable_controls;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.Screens.Menu.SelectGameMode
{
    public class SelectGameModeMenu : BaseMenuScreen<Config.Menu>
    {
        class MenuOptions
        {
            public MenuOptions()
            {
                Mode = GameMode.GameModes.None;
                ModeDescription = null;
            }

            public MenuOptions(GameMode.GameModes mode, string modeDescription)
            {
                Mode = mode;
                ModeDescription = modeDescription;
            }

            public GameMode.GameModes Mode { get; private set; }
            public string ModeDescription { get; private set; }
        }

        private static readonly MenuOptions[,] GameModeList = new[,]
                                                                     {
                                                                         { new MenuOptions(GameMode.GameModes.MoneyMayhem, "Money mayhem"), new MenuOptions(GameMode.GameModes.TeamMoneyMayhem, "Team Money mayhem") },
                                                                         { new MenuOptions(GameMode.GameModes.CaptureTheFlag, "Capture the flag"), new MenuOptions(GameMode.GameModes.InvadedDungeon, "Dungeon invasion") },
                                                                         { new MenuOptions(GameMode.GameModes.DeathMatch, "Death match"), new MenuOptions(GameMode.GameModes.TeamDeathMatch, "Team Death match") },
                                                                     };

        private readonly ImageContainer _cursorImage;

        private int _cursorX, _cursorY;
        private readonly Label _gameModeLabel;

        public SelectGameModeMenu(TE_Vector2 size)
        {

            _cursorX = 0;
            _cursorY = 0;

            var title = Title.Gray("Select a game mode", size);

            _cursorImage = new ImageContainer
            {
                Sprite = MenuSheet.GetSprite(MenuSheet.Menu.LetterSelected),
                Origin = TE_Rectangle.Alignment.CenterMiddle,
                Scale = new TE_Vector2(2.25f, 2.25f)
            };
            _cursorImage.AddAnimations(new ScaleAnimation(new SinusTrajectory(), new TE_Vector2(2.75f, 2.75f), 800, true, 0));

            _gameModeLabel = new Label(MenuSheet.GetInstance().GetSpriteAlphabet<MenuSheet.LetterAlphabet>, "")
                                 {
                                     PositionGridBound = new TE_Vector2(1, 5),
                                     Scale = new TE_Vector2(3, 3)
                                 };

            MoveCursor();


            var topLeftCorner = new TE_Vector2(size.X / 2 + GameModeList.GetLength(1) / 2.0f * -4 + (GameModeList.GetLength(1) - 1) / 2.0f * -1,
                                               (size.Y - 6) / 2 + 6 + GameModeList.GetLength(0)/2.0f*-4 + (GameModeList.GetLength(0) - 1)/2.0f*-1);

            var modeContainer = new Container(topLeftCorner);

            for (int y = 0; y < GameModeList.GetLength(0); y++)
            {
                for (int x = 0; x < GameModeList.GetLength(1); x++)
                {
                    if(GameModeList[y, x].Mode != GameMode.GameModes.None)
                        modeContainer.AddChildren(new ImageContainer
                                                      {
                                                          PositionGridBound = new TE_Vector2(5 * x, 5 * y),
                                                          Sprite = MenuSheet.GetSprite(GameModeList[y, x].Mode),
                                                      });
                }
            }
            modeContainer.AddChildren(_cursorImage);

            

            AddChildren(modeContainer, title, _gameModeLabel);
        }

        public GameMode.GameModes Mode
        {
            get
            {
                return GameModeList[_cursorY, _cursorX].Mode;
            }
        }

        void MoveCursor()
        {
            _cursorImage.PositionGridBound = new TE_Vector2(_cursorX * 5 + 2, _cursorY * 5 + 2);

            _gameModeLabel.SetText(GameModeList[_cursorY, _cursorX].ModeDescription);
        }

        public override void UpKey(int id)
        {
            do
            {
                _cursorY--;
                if (_cursorY < 0)
                    _cursorY = GameModeList.GetLength(0) - 1;
            } while (GameModeList[_cursorY, _cursorX] == null);

            MoveCursor();
        }

        public override void DownKey(int id)
        {
            do
            {
                _cursorY++;
                if (_cursorY >= GameModeList.GetLength(0))
                    _cursorY = 0;
            } while (GameModeList[_cursorY, _cursorX] == null);

            MoveCursor();
        }

        public override void LeftKey(int id)
        {
            do
            {
                _cursorX--;
                if (_cursorX < 0)
                    _cursorX = GameModeList.GetLength(1) - 1;
            } while (GameModeList[_cursorY, _cursorX] == null);

            MoveCursor();
        }

        public override void RightKey(int id)
        {
            do
            {
                _cursorX++;
                if (_cursorX >= GameModeList.GetLength(1))
                    _cursorX = 0;
            } while (GameModeList[_cursorY, _cursorX] == null);

            MoveCursor();
        }

        public override void AcceptKey(int id)
        {
            ChangeScreen(-1, Config.Menu.Start);
        }

        public override void CancelKey(int id)
        {

        }
    }
}